package com.sgb.goods.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.ContractInfoEntity;
import com.sgb.goods.entity.LogisticsDetailEntity;
import com.sgb.goods.entity.OrderListEntity;
import com.sgb.goods.entity.order.OrderPayInfoEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.OrderListAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单列表 viewModel
 *
 * @author cyj
 */
public class OrderListViewModel extends BaseViewModel {

    private int page = 1;
    public OrderListAdapter adapter;
    public MutableLiveData<List<OrderListEntity>> orderList = new MutableLiveData<>();
    /**
     * 弹窗系 MutableLiveData 更新UI
     */
    public MutableLiveData<OrderPayInfoEntity> orderPayDetail = new MutableLiveData<>();
    public MutableLiveData<ContractInfoEntity> contractInfo = new MutableLiveData<>();
    public MutableLiveData<LogisticsDetailEntity> logisticsDetail = new MutableLiveData<>();

    public OrderListViewModel() {
        adapter = new OrderListAdapter();
    }

    /**
     * 获取订单列表
     */
    public void getOrderListModel(String type, String orderState, String orderProcessState, boolean isLoadMore) {
        page = isLoadMore ? page + 1 : Constants.PAGE_NUM;
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getOrdersList(orderProcessState, page, Constants.PAGE_SIZE_10, type, orderState)
                .enqueue(new GoodsCallBack<BaseEntity<ListResponse<List<OrderListEntity>>>>() {

                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<OrderListEntity>>> data) {
                        orderList.setValue(data.getData().getList());
                        adapter.setOrderType(type);
                        if (isLoadMore) {
                            adapter.addData(data.getData().getList());
                        } else {
                            adapter.setNewData(data.getData().getList());
                        }
                    }

                    @Override
                    public void onFail() {
                        orderList.setValue(null);
                    }
                });
    }

    /**
     * 支付详情请求
     */
    public void getListPayDetails(OrderListEntity entity, String orderType) {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getListPayDetails(entity.id + "", "1".equals(orderType) ? "" : "2", "1").enqueue(
                new GoodsCallBack<BaseEntity<OrderPayInfoEntity>>() {
                    @Override
                    public void onSuccess(BaseEntity<OrderPayInfoEntity> data) {
                        data.getData().companyName = "1".equals(orderType) ? entity.supplierName : 1 == entity.orderType ? entity.ownedName : 0 == entity.orderType ? entity.personName + "(" + entity.personTel + ")" : "";
                        data.getData().orderId = entity.id + "";
                        data.getData().paymentWay = entity.paymentWay;
                        data.getData().paymentMethod = entity.paymentMethod;
                        data.getData().orderProcessState = entity.orderProcessState;
                        data.getData().orderIdentity = entity.orderType.intValue();
                        orderPayDetail.setValue(data.getData());
                    }
                });
    }

    /**
     * 获取合同详情
     */
    public void getContractDetailsByOrderId(OrderListEntity entity, String orderType) {
        String myCompanyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        Map<String, Object> map = new HashMap<>(4);
        map.put("orderNo", entity.id + "");
        map.put("companyNo", myCompanyNo);
        map.put("page", "1");
        map.put("limit", "20");
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getContractList(map).enqueue(new GoodsCallBack<BaseEntity<ContractInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<ContractInfoEntity> data) {
                data.getData().companyName = "1".equals(orderType) ? entity.supplierName : entity.ownedName;
                data.getData().orderId = entity.id + "";
                contractInfo.setValue(data.getData());
            }
        });
    }

    /**
     * 物流详情
     */
    public void getDeliverByOrderId(OrderListEntity entity, String orderType) {
        String orderId = entity.id + "";
        String companyName = "1".equals(orderType) ? entity.supplierName : 1 == entity.orderType ? entity.ownedName : 0 == entity.orderType ? entity.personName + "(" + entity.personTel + ")" : "";
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getDeliverByOrderId(orderId).enqueue(new GoodsCallBack<BaseEntity<List<LogisticsDetailEntity.Record>>>() {
            @Override
            public void onSuccess(BaseEntity<List<LogisticsDetailEntity.Record>> data) {
                logisticsDetail.setValue(new LogisticsDetailEntity(companyName, orderId, data.getData()));
            }
        });
    }
}
